package com.rocching.easychat.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.rocching.easychat.R;
import com.rocching.easychat.core.IApiCallback;
import com.rocching.easychat.core.ViewClickListener;
import com.rocching.easychat.model.UserModel;
import com.rocching.easychat.service.UserService;
import com.rocching.easychat.service.dto.LoginInput;
import com.rocching.easychat.service.dto.RegisterUserInput;
import com.rocching.easychat.util.StringUtil;

public class RegisterActivity extends AppCompatActivity {
    private Button btnBack;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
    }

    private void init() {
        btnBack = findViewById(R.id.btn_back);
        btnRegister = findViewById(R.id.btn_register);

        btnBack.setOnClickListener(new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                finish();
            }
        });

        btnRegister.setOnClickListener(new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                hideKeyboard(v.getWindowToken());

                EditText userAccount = findViewById(R.id.user_account);
                EditText userName = findViewById(R.id.user_name);
                EditText userPwd = findViewById(R.id.user_pwd);

                register(userAccount.getText().toString(), userName.getText().toString(), userPwd.getText().toString());
            }
        });
    }

    private void register(String account, String name, String pwd) {
        if (StringUtil.isEmpty(account)) {
            showText(getString(R.string.user_account_tip2));
            return;
        }
        if (StringUtil.isEmpty(name)) {
            showText(getString(R.string.user_name_tip2));
            return;
        }
        if (StringUtil.isEmpty(pwd)) {
            showText(getString(R.string.password_tip2));
            return;
        }

        RegisterUserInput input = new RegisterUserInput();
        input.setAccount(account);
        input.setName(name);
        input.setPassword(pwd);

        UserService.getInstance().register(input, new IApiCallback<UserModel>() {
            @Override
            public void onResponse(boolean succeeded, String body, UserModel data) {
                if (succeeded) {
                    finish();
                } else {
                    showText(body);
                }
            }
        });
    }

    private void showText(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}